#!/bin/bash
# 安装Maven

# 引入公共配置
source ./common.sh

# 确保应用目录存在
app_home

function install_maven() {
    local version=`echo ${Version} |awk -F. '{print $1}' `
    local appname="apache-maven-${Version}-bin.tar.gz"
    
    if [ ! -e "${APPS_HOME}/${appname}" ];then
        echo "正在下载Maven ${Version}..."
        wget -P ${APPS_HOME} ${MAVEN_NAME}${version}/${Version}/binaries/${appname}
        if [ $? -ne 0 ]; then
            error_exit "Maven下载失败"
        fi
    fi
    
    tar xf ${APPS_HOME}/${appname} -C ${Basedir}
    rm -rf ${Basedir}/maven
    mv ${Basedir}/apache-maven-${Version} ${Basedir}/maven
    rm -f /usr/local/bin/mvn
    ln -s ${Basedir}/maven/bin/mvn /usr/local/bin/
    
    check=$(mvn -v|grep "Apache Maven $Version"|wc -l)
    if [ $check -eq 0 ];then
        error_exit "maven未安装成功"
    else
        success_msg "maven安装成功，版本为:$Version"
    fi
}

echo "开始安装Maven..."

Maven_ALL_Version=$(curl -s https://mirrors.aliyun.com/apache/maven/maven-3/|grep -E '[0-9]+\.[0-9]+\.[0-9]+\/'|grep td|awk -F '>' '{print $3}'|tr -d '/</a')
Maven_DEF_Version=$(echo $Maven_ALL_Version|awk '{print $1}')
echo "可选版本如下: $Maven_ALL_Version"
echo "本源来自阿里云镜像仓库"

read -p "输入需要的maven版本(默认$Maven_DEF_Version): " Version
if [ -z "${Version}" ];then
    Version=$Maven_DEF_Version
    echo "使用默认版本: $Version"
fi

install_maven
